﻿using mediaPrime.Controls;
using mediaPrime.Plugin.SystemManagement.Dialogs;
using mediaPrime.Sdk;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace mediaPrime.Plugin.SystemManagement
{
    /// <summary>
    /// Interaktionslogik für MainUserControl.xaml
    /// </summary>
    public partial class MainUserControl : UserControl
    {
        private List<object> menuItemsMain;
        private List<object> menuItemsSettings;
        private List<object> menuItemsPlugins;

        private SystemPlugin plugin;

        public MainUserControl(SystemPlugin plugin)
        {
            InitializeComponent();

            this.plugin = plugin;

            showMainMenu();
        }

        private void sideMenu_MenuItemClicked(object sender, MPMenuItemClickedEventArgs e)
        {
            var menuItem = e.Item as MenuItem;

            if (menuItem != null)
            {

                var type = menuItem.Type;

                if (type == MenuItemType.Settings)
                {
                    showSettingsMenu();
                }
                else if (type == MenuItemType.Plugins)
                {
                    showPluginsMenu();
                }
                else if (type == MenuItemType.Plugin)
                {
                    showPluginSettings(menuItem.Tag as string);
                }
                else if (type == MenuItemType.Back)
                {
                    if (menuItem.Level == MenuItemLevel.Settings || menuItem.Level == MenuItemLevel.Plugins)
                        showMainMenu();
                    else if (menuItem.Level == MenuItemLevel.SettingsGroup)
                        showSettingsMenu();
                    else if (menuItem.Level == MenuItemLevel.Main)
                        plugin.GotToHome();
                }
                else if (type == MenuItemType.ChangeView)
                {
                    var setting = plugin.Settings.UserSettings["SettingsView"];
                    var dlg = new Dialogs.ChangeSettingsViewDialog(setting.GetValue<SettingEnumValue>("Standard").Value);
                    if (dlg.ShowDialog() == true)
                    {
                        setting.SetValue(dlg.View);
                        showPluginSettings(menuItem.Tag as string);
                    }
                }
            }

        }

        private void showMainMenu()
        {
            if (menuItemsMain == null)
            {
                menuItemsMain = new List<object>();
                menuItemsMain.Add(new MenuItem(Properties.Resources.strSettings, null, MenuItemLevel.Main, MenuItemType.Settings));
                menuItemsMain.Add(new MenuItem(Properties.Resources.strPlugins, null, MenuItemLevel.Main, MenuItemType.Plugins));
                menuItemsMain.Add(new MenuItem(Properties.Resources.strUpdates, null, MenuItemLevel.Main, MenuItemType.Updates));
                menuItemsMain.Add(new MenuItem(Properties.Resources.strInfo, new UserControls.InfoUserControl(plugin), MenuItemLevel.Main, MenuItemType.Info));
                menuItemsMain.Add(new MPMenuItemSeparator());
                menuItemsMain.Add(new MenuItem(Properties.Resources.strBack, null, MenuItemLevel.Main, MenuItemType.Back));
            }

            contentPresenter.Content = null;
            textBlockContentTitle.Text = null;

            textBlockSideMenuTitle.Text = Properties.Resources.strSystem;

            sideMenu.ItemsSource = menuItemsMain;
            sideMenu.SelectedItem = menuItemsMain[3];
        }

        private void showSettingsMenu()
        {
            if (menuItemsSettings == null)
            {
                menuItemsSettings = new List<object>();
                menuItemsSettings.Add(new MenuItem("mediaPrime", null, MenuItemLevel.Settings, MenuItemType.Plugin, "mediaPrime"));
                menuItemsSettings.Add(new MPMenuHeaderItem(Properties.Resources.strPluginsList));


                if (plugin.AvailablePlugins != null)
                {
                    foreach (var pluginInfo in plugin.AvailablePlugins)
                    {
                        // Check if settings are available for this plugin
                        if (Settings.Apps[pluginInfo.Id].UserSettings.Where(us => us.Scope != SettingScope.Internal).Count() > 0)
                            menuItemsSettings.Add(new MenuItem(pluginInfo.Name, null, MenuItemLevel.Settings, MenuItemType.Plugin, pluginInfo.Id));
                    }
                }

                menuItemsSettings.Add(new MPMenuItemSeparator());
                menuItemsSettings.Add(new MenuItem(Properties.Resources.strBack, null, MenuItemLevel.Settings, MenuItemType.Back));
            }

            contentPresenter.Content = null;
            textBlockContentTitle.Text = null;

            textBlockSideMenuTitle.Text = Properties.Resources.strSettings;

            sideMenu.ItemsSource = menuItemsSettings;
            sideMenu.SelectedItem = menuItemsSettings.FirstOrDefault();
        }

        private void showPluginSettings(string id)
        {
            var menuItems = new List<object>();
            var settingsViewEnum = plugin.Settings.UserSettings["SettingsView"].GetValue<SettingEnumValue>("Standard");
            var settingsView = (SettingScope)Enum.Parse(typeof(SettingScope), settingsViewEnum.Value);

            if (!string.IsNullOrEmpty(id))
            {
                var userSettings = Settings.Apps[id].UserSettings;
                var groups = userSettings.Where(us => us.Scope == SettingScope.Standard || us.Scope == settingsView).GroupBy(gn => gn.GroupName).Select(g => g.First()).Select(g => g.GroupName); // Get all available groups

                foreach (var group in groups)
                {
                    menuItems.Add(new MenuItem(group, new UserControls.SettingsUserControl(id, group), MenuItemLevel.SettingsGroup, MenuItemType.SettingsGroup));
                }
            }

            menuItems.Add(new MPMenuHeaderItem(Properties.Resources.strOptions));


            if (settingsView == SettingScope.Standard)
                menuItems.Add(new MenuItem(Properties.Resources.strViewStandard, null, MenuItemLevel.SettingsGroup, MenuItemType.ChangeView, id));
            else if (settingsView == SettingScope.Advanced)
                menuItems.Add(new MenuItem(Properties.Resources.strViewAdvanced, null, MenuItemLevel.SettingsGroup, MenuItemType.ChangeView, id));

            menuItems.Add(new MenuItem(Properties.Resources.strResetSettings, null, MenuItemLevel.SettingsGroup, MenuItemType.ResetSettings));

            menuItems.Add(new MPMenuItemSeparator());
            menuItems.Add(new MenuItem(Properties.Resources.strBack, null, MenuItemLevel.SettingsGroup, MenuItemType.Back));

            var pluginInfo = plugin.AvailablePlugins.Where(p => p.Id == id).FirstOrDefault();
            if (pluginInfo != null)
                textBlockSideMenuTitle.Text = pluginInfo.Name;
            else
                textBlockSideMenuTitle.Text = "mediaPrime";

            contentPresenter.Content = null;
            textBlockContentTitle.Text = null;

            sideMenu.ItemsSource = menuItems;
            sideMenu.SelectedItem = menuItems.FirstOrDefault();
        }
        private void showPluginsMenu()
        {
            if (menuItemsPlugins == null)
            {
                menuItemsPlugins = new List<object>();
                menuItemsPlugins.Add(new MenuItem(Properties.Resources.strInstalledPlugins, new UserControls.PluginsUserControl(plugin.AvailablePlugins), MenuItemLevel.Plugins, MenuItemType.Plugins));
                menuItemsPlugins.Add(new MPMenuItemSeparator());
                menuItemsPlugins.Add(new MenuItem(Properties.Resources.strBack, null, MenuItemLevel.Plugins, MenuItemType.Back));
            }

            contentPresenter.Content = null;
            textBlockContentTitle.Text = null;
            textBlockSideMenuTitle.Text = Properties.Resources.strPlugins;

            sideMenu.ItemsSource = menuItemsPlugins;
            sideMenu.SelectedItem = menuItemsPlugins.FirstOrDefault();
        }

        private void sideMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                var selectedItem = e.AddedItems[0] as MenuItem;
                if (selectedItem != null && selectedItem.UserControl != null)
                {
                    contentPresenter.Content = selectedItem.UserControl;
                    textBlockContentTitle.Text = selectedItem.Title;
                }

            }
        }




    }

    public class MenuItem
    {
        public string Title { get; set; }
        public UserControl UserControl { get; set; }

        public MenuItemLevel Level { get; set; }
        public MenuItemType Type { get; set; }

        public object Tag { get; set; }

        public MenuItem(string title, UserControl userControl, MenuItemLevel level, MenuItemType type, object tag = null)
        {
            Title = title;
            UserControl = userControl;
            Level = level;
            Type = type;
            Tag = tag;
        }

        public override string ToString()
        {
            return Title;
        }
    }

    public enum MenuItemType
    {
        Info, Updates, Settings, Plugins, Plugin, SettingsGroup, ChangeView, ResetSettings, Back
    }

    public enum MenuItemLevel
    {
        Main, Settings, Plugins, SettingsGroup
    }
}
